package com.javaProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaProject.dto.R;
import com.javaProject.entity.Admin;
import com.javaProject.entity.Teacher;
import com.javaProject.service.AdminService;
import com.javaProject.mapper.AdminMapper;
import com.javaProject.service.UserkeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Objects;

/**
* @author ASUS
* @description 针对表【admin】的数据库操作Service实现
* @createDate 2024-03-19 14:09:19
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    @Autowired
    SM2 sm2;

    @Autowired
    UserkeyService keyService;

    @Override
    public R login(Admin admin) throws NoSuchAlgorithmException, InvalidKeySpecException {
        QueryWrapper<Admin> query= Wrappers.query(Admin.class);
        query.eq("username",admin.getUsername());
        Admin one = getOne(query);
        if (Objects.isNull(one)){
            return R.fail().setMsg("用户不存在").setCode(-2);
        }
        String password = sm2.decrypt(one.getPassword(),keyService.getPrivateKey(admin.getUsername()));
        System.out.println(password);
        if(Objects.equals(admin.getPassword(),password)){
            return R.ok().setData(one);
        }
        return R.fail().setMsg("密码错误").setCode(-1);
    }
}




